package com.ruoyi.trans.utils;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CsvParser {
    public static class CsvResult {
        private final List<String> headers;
        private final List<Map<String, String>> dataList;

        public CsvResult(List<String> headers, List<Map<String, String>> dataList) {
            this.headers = headers;
            this.dataList = dataList;
        }

        public List<String> getHeaders() {
            return headers;
        }

        public List<Map<String, String>> getDataList() {
            return dataList;
        }
    }

    public static CsvResult parseCsv(MultipartFile file) throws IOException, CsvValidationException {
        List<String> headers = new ArrayList<>();
        List<Map<String, String>> dataList = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
             CSVReader csvReader = new CSVReader(reader)) {
            // 读取表头
            String[] headerArray = csvReader.readNext();
            if (headerArray != null) {
                for (String header : headerArray) {
                    headers.add(header.trim());
                }
            }

            // 读取数据
            String[] nextLine;
            while ((nextLine = csvReader.readNext()) != null) {
                Map<String, String> rowData = new HashMap<>();
                for (int i = 0; i < headers.size(); i++) {
                    if (i < nextLine.length) {
                        rowData.put(headers.get(i), nextLine[i]);
                    }
                }
                dataList.add(rowData);
            }
        }

        return new CsvResult(headers, dataList);
    }

    /**
     * 单独获取 CSV 文件的表头
     * @param file 上传的 CSV 文件
     * @return 表头列表
     * @throws IOException 读取文件时发生的 IO 异常
     * @throws CsvValidationException CSV 验证异常
     */
    public static List<String> getHeaders(MultipartFile file) throws IOException, CsvValidationException {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
             CSVReader csvReader = new CSVReader(reader)) {
            String[] headerArray = csvReader.readNext();
            List<String> headers = new ArrayList<>();
            if (headerArray != null) {
                for (String header : headerArray) {
                    headers.add(header.trim());
                }
            }
            return headers;
        }
    }

    // 其他已有方法保持不变
    public static void parseCsvByBatch(MultipartFile file, int batchSize, BatchProcessor processor) throws IOException, CsvValidationException {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
             CSVReader csvReader = new CSVReader(reader)) {
            // 读取表头
            String[] headerArray = csvReader.readNext();
            List<String> headers = new ArrayList<>();
            if (headerArray != null) {
                for (String header : headerArray) {
                    headers.add(header.trim());
                }
            }

            List<Map<String, String>> batchData = new ArrayList<>();
            String[] nextLine;
            while ((nextLine = csvReader.readNext()) != null) {
                Map<String, String> rowData = new HashMap<>();
                for (int i = 0; i < headers.size(); i++) {
                    if (i < nextLine.length) {
                        rowData.put(headers.get(i), nextLine[i]);
                    }
                }
                batchData.add(rowData);

                if (batchData.size() >= batchSize) {
                    processor.processBatch(headers, batchData);
                    batchData.clear();
                }
            }

            // 处理最后一批数据
            if (!batchData.isEmpty()) {
                processor.processBatch(headers, batchData);
            }
        }
    }

    /**
     * 数据处理接口
     */
    public interface BatchProcessor {
        void processBatch(List<String> headers, List<Map<String, String>> batchData);
    }
}